<?php
declare(strict_types=1);
namespace SixShop\Eav\Controller;

use SixShop\Core\Request;
use SixShop\Eav\Entity\EavEntityTypeEntity;
use think\Response;

use function SixShop\Core\success_response;

class EntityTypeController
{
    public function index(EavEntityTypeEntity $entityTypeEntity): Response
    {
        return success_response($entityTypeEntity->select());
    }

    public function read(int $id, EavEntityTypeEntity $entity): Response
    {
        return success_response($entity->find($id));
    }
    public function save(Request $request, EavEntityTypeEntity $entity): Response
    {
        $data = $request->post();
        $entityObject = $entity->withTrashed()
            ->where('entity_type_code', $data['entity_type_code'])
            ->findOrEmpty();
        if (!$entityObject->isEmpty()) {
            $entityObject->restore();
            $data['id'] = $entityObject['id'];
        }
        return success_response($entityObject->save($data));
    }

    public function update(int $id, Request $request, EavEntityTypeEntity $entity): Response
    {
        return success_response($entity->update($request->post(), ['id' => $id]));
    }
    public function delete(int $id, EavEntityTypeEntity $entity): Response
    {
        return success_response($entity->destroy($id));
    }
}